package com.demo.order.controller;

import com.demo.order.DTO.CartDTO;
import com.demo.order.client.ProductClient;
import com.demo.order.dataobject.ProductInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

@RestController
@Slf4j
public class ClientController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ProductClient productClient;

    @GetMapping("/getProductMsg")
    public String getProductMsg(){

        // 1.第一种方式
       /* RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject("http://localhost:8662/msg", String.class);
        log.info("【远程调用】，response={}", response);*/

        String response = restTemplate.getForObject("http://PRODUCT/msg", String.class);
        log.info("【远程调用】，response={}", response);
        return response;
    }

    @GetMapping("/getProductMsg2")
    public String getProductMsg2(){
        String response = productClient.msg();
        log.info("response = {}", response);
        return response;
    }

    @GetMapping("/product")
    public String listForOrder(){
        List<ProductInfo> productInfoList = productClient.findList(Arrays.asList("1230485723457322"));
        log.info("response={}", productInfoList);
        return "OK";
    }

    @GetMapping("/productdecreaseStock")
    public String productdecreaseStock(){
        productClient.decreaseStock(Arrays.asList(new CartDTO("1230485723457322", 10)));
        return "OK";
    }
}
